package kis.io.xml;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.LineSeparator;
import org.jdom2.output.XMLOutputter;
import org.jdom2.output.Format.TextMode;
import org.junit.Test;

public class TestJdom2 {
	String path = this.getClass().getResource("/").toString().replace("file:", "") + "TestXml_jdom2.xml";
	@Test
	public void testJdom2Write()
	{
		Element root = new Element("package");
		Document doc = new Document(root);
		Element shop = new Element("shop");
		shop.setAttribute("shopName", "新华书店");
		shop.addContent(new Element("book").setText("java编程思想").setAttribute("id", "1"));
		shop.addContent(new Element("book").setText("HeadFirstJava").setAttribute("id", "2"));
		root.addContent(shop);
		Element shop1 = new Element("shop");
		shop1.addContent(new Element("book").setText("java编程思想").setAttribute("id", "1"));
		root.addContent(shop1);
		Format f = Format.getRawFormat();
		f.setIndent(null);
		f.setEncoding("GBK");
		f.setLineSeparator(LineSeparator.NONE);//去掉所有空格
		XMLOutputter putter = new XMLOutputter(f);
		try
		{
			System.out.println(path);
			putter.output(doc, new FileOutputStream(path));
			System.out.println(new FileInputStream(path).available());
		} catch (FileNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
